package com.longge.wechat.utils;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageMake {

	/**
	 * 给图片添加水印
	 * 
	 * @param iconPath 水印图片路径
	 * @param srcImgPath 源图片路径
	 * @param targerPath 目标图片路径
	 * @param text 添加的文字
	 */
	public static void markImageAndText(String iconPath, String srcImgPath,
			String targerPath, String text) {
		changeImageSize(srcImgPath, targerPath);
		markImageByIcon(iconPath, targerPath, targerPath, null);
		markImageByText(targerPath, targerPath, text);
	}
	
	/**
     * 生成图片缩略图
     *
     * @param srcImgPath 原图片文件
     * @param targerPath 目标图片文件
     * 
     */
    public static void changeImageSize(String srcImgPath,String targerPath) {
    	try {
            File fi = new File(srcImgPath);
            File fo = new File(targerPath);
            AffineTransform transform = new AffineTransform();
            BufferedImage bis = ImageIO.read(fi);
            int w = bis.getWidth();
            int h = bis.getHeight();
            double sx = (double)0.5;
            double sy = (double)0.5;
           /* //如果图片宽度不够，增加宽度，同时增加长度（按比例）
            if(w < 600){
            	int index = (int)600/w;
            	w = 600;
            	h = (int)index * h;
            	sx = (double)index;
            	sy = (double)index;
            }*/
            int w1 = (int)(sx * w);
            int h1 = (int)(sy * h);
            transform.setToScale(sx,sy);
            AffineTransformOp ato = new AffineTransformOp(transform, null);
            BufferedImage bid = new BufferedImage(w1, h1, BufferedImage.TYPE_3BYTE_BGR);
            ato.filter(bis,bid);
            ImageIO.write(bid, "jpeg", fo);
            System.out.println("图片完成缩放。。。。。。");
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

	/**
	 * 给图片添加水印、可设置水印图片旋转角度
	 * 
	 * @param iconPath 水印图片路径
	 * @param srcImgPath 源图片路径
	 * @param targerPath 目标图片路径
	 * @param degree 水印图片旋转角度
	 */
	public static void markImageByIcon(String iconPath, String srcImgPath,
			String targerPath, Integer degree) {
		OutputStream os = null;
		try {
			Image srcImg = ImageIO.read(new File(srcImgPath));

			BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
					srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

			// 得到画笔对象
			// Graphics g= buffImg.getGraphics();
			Graphics2D g = buffImg.createGraphics();

			// 设置对线段的锯齿状边缘处理
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BILINEAR);

			g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),
					srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

			if (null != degree) {
				// 设置水印旋转
				g.rotate(Math.toRadians(degree),
						(double) buffImg.getWidth() / 2,
						(double) buffImg.getHeight() / 2);
			}

			// 水印图象的路径 水印一般为gif或者png的，这样可设置透明度
			ImageIcon imgIcon = new ImageIcon(iconPath);

			// 得到Image对象。
			Image img = imgIcon.getImage();

			float alpha = 1f; // 透明度
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

			// 表示水印图片的位置
			g.drawImage(img, 512, 30, null);

			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

			g.dispose();

			os = new FileOutputStream(targerPath);

			// 生成图片
			ImageIO.write(buffImg, "JPG", os);

			System.out.println("水印完成添加。。。。。。");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != os)
					os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 给图片添加文字水印
	 * 
	 * @param srcImgPath 源图片路径
	 * @param targerPath 目标图片路径
	 * @param text 添加的文字
	 */
	public static void markImageByText(String srcImgPath, String targerPath, String text){
			
		try {
			ImageIcon imgIcon = new ImageIcon(srcImgPath);
			Image theImg = imgIcon.getImage();
			int width = theImg.getWidth(null);
			int height = theImg.getHeight(null);
			BufferedImage bimage = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bimage.createGraphics();
			g.drawImage(theImg, 0, 0, null);
			g.setBackground(Color.white);
			g.setColor(Color.black);
			g.setFont(new Font("宋体", Font.BOLD, 24));
			// 设置水印旋转
			//g.rotate(Math.toRadians(30), 506, 90);
			g.drawString(text.substring(0, 1), 505, 100);
			g.drawString(text.substring(1, 2), 517, 110);
			g.drawString(text.substring(2, 3), 531, 119);
			g.drawString(text.substring(3, 4), 545, 123);
			g.drawString(text.substring(4, 5), 559, 119);
			g.drawString(text.substring(5, 6), 573, 110);
			g.drawString(text.substring(6, 7), 585, 100);
			
			g.dispose();
		
			ImageIO.write(bimage, "PNG", new File(targerPath));
			System.out.println("文字完成添加。。。。。。");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*String srcImgPath = "F:/demo/20131227152752_922.jpg";
		String iconPath = "F:/demo/mark_icon.png";
		String targerPath = "F:/demo/mark/20131227152752_922.jpg";
		String text = "8888888";
		// 给图片添加水印
		ImageMake.markImageAndText(iconPath, srcImgPath, targerPath, text);*/
		//System.out.println(ImageMake.class.getResource("/").getPath());
		//changeImageSize("F:/workspace/wechat/WebContent/resources/images/1.jpg", "F:/workspace/wechat/WebContent/resources/images/new/1.jpg");
		for (int i = 1; i < 108; i++) {
			String fromPath = "F:/workspace/wechat/WebContent/resources/images/image ("+i+").jpg";
			//System.out.println(fromPath);
			String toPath = "F:/workspace/wechat/WebContent/resources/images/new/image ("+i+").jpg";
			//System.out.println(toPath);
			changeImageSize(fromPath,toPath);
		}
	}
}
